﻿using System.Collections.Generic;
using System.IO;
using System.Text;

namespace HtmlDeploy
{
    public class JqueryTemplate
    {
        private readonly string externalPath;

        public JqueryTemplate(string externalPath)
        {
            this.externalPath = Path.GetFullPath(externalPath);
        }

        public string Process()
        {
            var filePaths = new List<string>();

            //Add sub dir html files
            foreach (var directory in Directory.GetDirectories(externalPath, "*", SearchOption.AllDirectories))
                filePaths.AddRange(Directory.GetFiles(directory, "*.html"));

            //Add current dir html files
            filePaths.AddRange(Directory.GetFiles(externalPath, "*.html"));

            var sb = new StringBuilder();

            foreach (var filePath in filePaths)
            {
                using (var reader = new StreamReader(filePath))
                {
                    var info = new FileInfo(filePath);
                    var id = info.FullName.Replace(externalPath, "").Replace("\\", "-").Replace(".html", "");

                    sb.AppendLine(string.Format(@"<script id=""tmpl{0}"" type=""text/x-jquery-tmpl"">", id));
                    sb.AppendLine(reader.ReadToEnd());
                    sb.AppendLine("</script>");
                    sb.AppendLine("");
                }
            }

            return sb.ToString();
        }
    }
}
